package com.itheima.service.db.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.domain.db.AnalysisByDay;
import com.itheima.domain.vo.AnalysisSummaryVo;
import com.itheima.mapper.AnalysisByDayMapper;
import com.itheima.mapper.LogMapper;
import com.itheima.service.db.AnalysisService;
import com.itheima.util.ComputeUtil;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.Date;

@DubboService
public class AnalysisServiceImpl implements AnalysisService {
    @Autowired
    private LogMapper logMapper;

    @Autowired
    private AnalysisByDayMapper analysisByDayMapper;

    @Override
    public void logToAnalysisByDay() {
        String today = DateUtil.today();
        String yesterday = DateUtil.offsetDay(new Date(), -1).toDateStr();
//        String yesterday2 = DateUtil.yesterday().toDateStr();
        //判断今天有没有分析数据有就修改没有就添加

        //查询今日注册
        Integer registerNum = logMapper.findByLoginOrRegister("0102", today);
        //查询今日登录
        Integer loginNum = logMapper.findByLoginOrRegister("0101", today);

        //查询活跃用户
        Integer activeNum = logMapper.findActivebyLogTime(today);
        //查询次日留存
        Integer retentionNum = logMapper.findRetention(yesterday, today);
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("record_date", today);
        AnalysisByDay analysisByDay = analysisByDayMapper.selectOne(queryWrapper);
        if (analysisByDay == null) {
            //凌晨无数据 执行增加
            analysisByDay = new AnalysisByDay();
            analysisByDay.setRecordDate(new Date());//统计时间
            analysisByDay.setNumRegistered(registerNum);//今日注册
            analysisByDay.setNumActive(activeNum);//活跃
            analysisByDay.setNumLogin(loginNum);//登录
            analysisByDay.setNumRetention1d(retentionNum);//次日留存
            analysisByDayMapper.insert(analysisByDay);
        } else {
            //执行修改
            analysisByDay.setNumRegistered(registerNum);//今日注册
            analysisByDay.setNumActive(activeNum);//活跃
            analysisByDay.setNumLogin(loginNum);//登录
            analysisByDay.setNumRetention1d(retentionNum);//次日留存
            analysisByDayMapper.updateById(analysisByDay);
        }
    }

    ////首页统计
    @Override
    public AnalysisSummaryVo findAnalysisSummaryVo() {
        String today = DateUtil.today();
        String yesterday = DateUtil.offsetDay(new Date(), -1).toDateStr();

        AnalysisByDay todatAnalysis = analysisByDayMapper.findByDateTime(today);
        AnalysisByDay yesterdayAnalysis = analysisByDayMapper.findByDateTime(yesterday);

        AnalysisSummaryVo vo = new AnalysisSummaryVo();

        vo.setCumulativeUsers(analysisByDayMapper.findCountSumRegister());//累计用户
        String weekDate = DateUtil.offsetDay(new Date(), -7).toDateStr();
        String monthDate = DateUtil.offsetDay(new Date(), -30).toDateStr();


        vo.setActivePassMonth(analysisByDayMapper.findCountSubActive(monthDate, today));//过去30天活跃
        vo.setActivePassWeek(analysisByDayMapper.findCountSubActive(weekDate, today));//过去7天活跃

        vo.setNewUsersToday(todatAnalysis.getNumRegistered().longValue());//今日新增用户数量
        //今日新增用户涨跌率，单位百分数，正数为涨，负数为跌   BigDecimal : 商业数字格式
        vo.setNewUsersTodayRate(ComputeUtil.computeRate(todatAnalysis.getNumRegistered(), yesterdayAnalysis.getNumRegistered()));

        //今日登录次数
        vo.setLoginTimesToday(todatAnalysis.getNumLogin().longValue());
        //今日登录次数涨跌率，单位百分数，正数为涨，负数为跌
        vo.setLoginTimesTodayRate(ComputeUtil.computeRate(todatAnalysis.getNumLogin(), yesterdayAnalysis.getNumLogin()));

        //今日活跃用户数量
        vo.setActiveUsersToday(todatAnalysis.getNumActive().longValue());
        //今日活跃用户涨跌率，单位百分数，正数为涨，负数为跌
        vo.setActiveUsersTodayRate(ComputeUtil.computeRate(todatAnalysis.getNumActive(), yesterdayAnalysis.getNumActive()));

        return vo;
    }
}
